package com.situ.company.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.company.entity.Department;
import com.situ.company.entity.Employee;
import com.situ.company.service.IDepartmentService;
import com.situ.company.service.IEmployeeService;
import com.situ.company.service.impl.DepartmentSeriviceImpl;
import com.situ.company.service.impl.EmployeeServiceImpl;
import com.situ.company.util.JSONResult;
import com.situ.company.util.JSONUtil;

@WebServlet("/employee")
public class EmployeeServlet extends HttpServlet{
	private IEmployeeService employeeService = new EmployeeServiceImpl();
	private IDepartmentService departmentService = new DepartmentSeriviceImpl();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String method = req.getParameter("method");
		
		// 如果没有传递method参数，默认执行查找所有
		if (method == null || "".equals(method)) {
			//method = "findAll";
			method = "selectAll";
		}
		
		switch (method) {
		case "selectAll":
			selectAll(req, resp);
			break;
		case "del":
			del(req, resp);
			break;
		case "add":
			add(req, resp);
			break;
		case "getUpdatePage":
			getUpdatePage(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		case "getAddPage":
			getAddPage(req, resp);
			break;
		default:
			break;
		}
	}

	private void getAddPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ArrayList<Department> list = departmentService.selectAll();
		
		req.setAttribute("list", list);
		req.getRequestDispatcher("/web/page/employee/add.jsp").forward(req, resp);
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		String code = req.getParameter("code");
		String name = req.getParameter("name");
		String pass = req.getParameter("pass");
		Employee employee = new Employee(Integer.parseInt(id), code, name, pass, "");
		
		JSONResult jsonResult = employeeService.update(employee);
		// 转换成json
		JSONUtil.obj2Json(jsonResult, resp);
	}

	// 通过转发的方式跳转到update.jsp页面
	private void getUpdatePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String id = req.getParameter("id");
		Employee employee = employeeService.selectById(Integer.parseInt(id));
		
		req.setAttribute("employee", employee);
		req.getRequestDispatcher("/web/page/employee/update.jsp").forward(req, resp);
	}

	private void add(HttpServletRequest req, HttpServletResponse resp) {
		String code = req.getParameter("code");
		String name = req.getParameter("name");
		String pass = req.getParameter("pass");
		String codeDept = req.getParameter("codeDept");
		
		Employee employee = new Employee(code, name, pass, codeDept);
		JSONResult jsonResult = employeeService.add(employee);
		// 转换成json
		JSONUtil.obj2Json(jsonResult, resp);
	}

	private void del(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("EmployeeServlet.del()");
		String code = req.getParameter("code");
		
		JSONResult jsonResult = employeeService.del(code);
		JSONUtil.obj2Json(jsonResult, resp);
	}

	private void selectAll(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("EmployeeServlet.selectAll()");
		// 得到搜索的名字
		String searchName = req.getParameter("name");
		if (searchName == null) {
			searchName = "";
		}
		
		JSONResult jsonResult = employeeService.selectAll(searchName);
		JSONUtil.obj2Json(jsonResult, resp);
	}
}
